package com.demo.controller;

import com.alibaba.fastjson2.JSON;
import com.demo.entity.TestEntity;
import com.demo.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangfengchen
 */
@Slf4j
@RestController
public class TestController {

  @Autowired
  private TestService testService;


  @Transactional(rollbackFor = Exception.class)
  @GetMapping("/test")
  public String test() {
    TestEntity test = new TestEntity();
    test.setId(1l);
    test.setUsername("五王");
    test.setAge(18);
    test.setPhone("123");
    // 保存时会根据实体类的每个属性进行非空判断，只有非空的属性对应的字段才会出现到SQL语句中
    boolean result = testService.save(test);
    log.info("结果，result = {}", result);
    // 获取ID
    Long id = test.getId();
    log.info("id = {}", id);
    TestEntity testEntity = testService.getById(1l);
    log.info("testEntity = {}", JSON.toJSONString(testEntity));
    return "SUCCESS";
  }

  @Transactional(rollbackFor = Exception.class)
  @GetMapping("/test1")
  public String test1() {
    TestEntity test = new TestEntity();
    test.setId(1L);
    test.setUsername("六王");
    test.setAge(18);
    test.setPhone("123");
    // 保存时会根据实体类的每个属性进行非空判断，只有非空的属性对应的字段才会出现到SQL语句中
    boolean result = testService.updateById(test);
    log.info("结果，result = {}", result);
    // 获取ID
    Long id = test.getId();
    log.info("id = {}", id);
    TestEntity testEntity = testService.getById(1L);
    log.info("testEntity = {}", JSON.toJSONString(testEntity));
    return "SUCCESS";
  }



}

